﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ManagementConsole;
using SharpShare.Management.Mmc.Views;
using SharpShare.Configuration;
using SharpShare.Activation;
using System.Configuration;

namespace SharpShare.Management.Mmc.Nodes {
    public class ExtensionsNode<TExtension> : ScopeNode where TExtension : IExtension {
        public ExtensionsNode(IExtensionFactory<TExtension> factory) {
            this.Factory = factory;
        }

        public IExtensionFactory<TExtension> Factory { get; private set; }
        public int? InstanceImageIndex { get; set; }
        public IExtensionInstanceSelector<TExtension> InstanceSelector { get; set; }

        protected virtual ExtensionNode<TExtension> CreateExtensionNode(ExtensionDescriptor<TExtension> descriptor) {
            ExtensionNode<TExtension> node = new ExtensionNode<TExtension>(this.Factory, descriptor) {
                ImageIndex = this.ImageIndex,
                SelectedImageIndex = this.SelectedImageIndex,
                InstanceImageIndex = this.InstanceImageIndex,
                InstanceSelector = this.InstanceSelector
            };
            return node;
        }
        protected override void OnExpand(AsyncStatus status) {
            base.OnExpand(status);

            this.Children.Clear();

            this.Children.AddRange(
                this.Factory.GetDescriptors()
                    .Select(d=> CreateExtensionNode(d))
                    .ToArray()
            );
        }


    }
}
